package net.proef.sample.controller;

import java.util.HashMap;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/stats")
public class StatisticsController {
	private Logger logger = LoggerFactory.getLogger(StatisticsController.class);

	@RequestMapping("/")
	public ModelAndView getStatistics() {
		ModelAndView mav = new ModelAndView("xmlView");
		Map<String, Object> data = new HashMap<String, Object>();

		CacheManager manager = CacheManager.getInstance();

		for (String cacheName : manager.getCacheNames()) {
			Cache cache = manager.getCache(cacheName);

			data.put(cacheName, cache.getStatistics());
			logger.debug("Added '{}' to the model", cacheName);
		}

		data.put("status", manager.getStatus());

		mav.addObject("data", data);

		return mav;
	}
}
